// import http from '@/shopro/request';
import {
	getToken
} from '@/utils/token.js'
import store from '@/store/index.js'
import {
	duplicateRemoval
} from "@/utils/common.js"
import {
	saveAssoComment,
	deleteAssoComment,
	selectContent,
	saveAssoCollection,
	saveAssoLike
} from '@/common/api/news.js'
export default class common {
	constructor(initData = {
		id: 1,
		type: '',
		detail: {}
	}, callback) {
		console.log('initData', initData);
		//评论列表
		this.commentData = {
			comment: [],
			status: ''
		}
		console.log('1111');
		this.currIdF = 0; //评论滚动的id
		this.currPage = 1;
		this.callback = callback; //回调函数
		this.acId = initData.id;
		this.entityName = initData.type;
		console.log('2222');
		this.detail = initData.detail;
		this.openComm();
		console.log('3333');
	}
	// 初始化
	async openComm() {
		console.log('aaaa');
		this.currPage = 1;
		this.commentData.status = 'loading';
		this.currId = 0;
		this.commentData.comment = [];
		//获取评论列表
		let res = await this.selectContent();
		console.log('获取评论列表respage', res.data.page);
		if (res.data.code) {
			this.currPage = res.data.page;
			this.commentData.status = res.data.page > this.currPage ? 'more' : 'nomore';
			let data = res.data.rows;
			console.log('data', data);
			let arrSend = this.initList(data);
			console.log('arrSend', arrSend);
			this.commentData.comment = duplicateRemoval(this.commentData.comment, arrSend, 'id');
		}
		this.callback({type:'commData',data:{
			commentData:this.commentData
		}})
	}
	initList(data){
		let arrSend = []
		data && data.forEach(m=>{
			let obj = {
				content:m.content,
				userNo:m.userNo,
				reDate:m.reDate,
				id:m.id,
				assoCommentList:[]
			}
			if(m.txShareContentItemList && m.txShareContentItemList.length){//采用递归方式初始化子数组
				m.assoCommentList = this.initList(m.txShareContentItemList)
				obj = {
					...obj,
					assoCommentList:m.assoCommentList
				}
			}
			
			if(m.usrCraftsman && m.usrCraftsman.craftsmanId){
				obj = {
					...obj,
					headImg:m.usrCraftsman.headImg,
					usrName:m.usrCraftsman.usrName,
					headImgCode:130
				}
			}
			else if(m.usrCompany && m.usrCompany.companyNameCn){
				obj = {
					...obj,
					headImg:m.usrCompany.companyLogo,
					usrName:m.usrCompany.companyNameCn,
					headImgCode:126
				}
			}
			arrSend.push(obj)
		})
		return arrSend
	}
	selectContent() { // 获取评论列表
		let params = {
			entityName: this.entityName,
			'page.current': this.currPage,
			shareId: this.acId
		}
		return selectContent(params);
	}
	
	// 删除评论
	delCom(e){
		if(!store.state.user.isLogin){
			store.dispatch('showAuthModal', 'accountLogin');
			return;
		}
		deleteAssoComment({id:e.id}).then(res=>{
			//console.log(res)
			res = res.data
			if(res.code){
				uni.showToast({
					title:'删除成功',
					icon:'none'
				})
				if(e.index2 === 0 || e.index2){
					this.commentData.comment[e.index].assoCommentList.splice(e.index2,1)
				}else{
					this.commentData.comment.splice(e.index,1)
				}
			}else{
				uni.showToast({
					title:'删除出错',
					icon:'none'
				})
			}
		})
		this.callback({type:'commData',data:{
			commentData:this.commentData
		}})
	}
	//获取下一页评论
	async getLast(){
		this.commentData.status = 'loading';
		this.currPage++;
		let res = await this.selectContent();
		res = res.data;
		if (res.code == 200) {
			this.currPage = res.listData['page.current'];
			res.pages > this.currPage ? this.commentData.status ='more' : this.commentData.status ='nomore';
			this.commentData.comment = tools.duplicateRemoval(this.commentData.comment, res.listData.records, 'id');
		}
	}
	//添加评论
	add(e) {
		if(!store.state.user.isLogin){
			store.dispatch('showAuthModal', 'accountLogin');
			return;
		}
		let params = {
			userNo: getToken('useNum'),
			entityName: this.entityName,
			shareId: this.detail.id,
			content: e.content,
			parentId:-1,
			typeId:this.detail.classTypeId
		}
		if (e.pId) { //第二级评论
			params['fromName'] = e.fromName || '';
			params['parentId'] = e.pId;
			params['fromUid'] = e.fromUid;
		}
		saveAssoComment(params).then(res => {
			res = res.data
			if(res.code){
				let item = this.initList([res.data])[0]
				if (!e.pId) {
					item.parentId == -1
					let list = [...[item], ...this.commentData.comment]
					this.commentData.comment = list
				} else {
					let idx = this.commentData.comment.findIndex(v => {
						return v.id == e.pId
					})
					if (idx != -1) {
						let currItem = this.commentData.comment[idx]
						if (currItem.assoCommentList && currItem.assoCommentList.length) {
							currItem.assoCommentList.push(item)
						} else {
							currItem.assoCommentList = [item]
						}
					}
				}
				setTimeout(() => {
					this.currIdF = item.id
				}, 200)
			}
		})
		this.callback({type:'commData',data:{
			commentData:this.commentData
		}})
		this.callback({type:'currIdF',data:{
			currIdF:this.currIdF
		}})
		
	}
	
	//点赞
	memberLike(){
		//console.log(store.state.user.isLogin)
		if(!store.state.user.isLogin){
			store.dispatch('showAuthModal', 'accountLogin');
			return;
		}
		saveAssoLike({
			userNo:getToken('useNum'),
			fromId:this.acId,
			typeId:this.entityName
		},'加载中').then(res=>{
			res = res.data
			if (res.code) {
				if (res.data && res.data.recordId) {
					this.detail.usrThumbsStatus = 1
					this.detail.commentNum++
					this.detail.loveQty++
				} else {
					this.detail.usrThumbsStatus = 0
					this.detail.commentNum--
					this.detail.loveQty--
				}
			}
		})
		this.callback({type:'detailData',data:{
			detailData:this.detail
		}})
	}
	// 收藏
	collectionPost(){
		if(!store.state.user.isLogin){
			store.dispatch('showAuthModal', 'accountLogin');
			return;
		}
		saveAssoCollection({
			usrNo:getToken('useNum'),
			collectionType:'adbCollege',
			msgId:this.acId
		},'加载中').then(res=>{
			res = res.data
			if (!this.detail.countStatus) {
				this.detail.countStatus = 1
				this.detail.saveQty++
				this.detail.count++
			} else {
				this.detail.countStatus = 0
				this.detail.saveQty--
				this.detail.count--
			}
		})
		this.callback({type:'detailData',data:{
			detailData:this.detail
		}})
	}
}
